-- UIDragonWish
-- Create by chengb Feb/5/2015
-- 龙珠许愿界面

UIDragonWish = class("UIDragonWish", function()
    return cc.Layer:create();
end);

function UIDragonWish.create(options)
    return UIDragonWish.new(options);
end

-- 选项数量
local MAX_OPTION_NUM = 3;

-- 构造函数
function UIDragonWish:ctor(options)
    self:setName("UIDragonWish");

    -- 初始化
    local node = cc.CSLoader:createNode("layout/dragon/DragonWish.csb");
    self.node = node;
    self:addChild(node);

    self:setLocalZOrder(UIMgr.TOP_MENU_ZORDER + 1);

    -- 确保选项数量是正确的
    assert(type(options) == "table" and (#options == MAX_OPTION_NUM), "选项数量不正确");

    -- 记录选项
    self.options = options;

    -- 初始化窗口
    self:initForm();

    -- 注册事件处理回调函数
    self:registerEventCallback();

    -- 重绘
    self:redraw();

    -- 适配
    self:resize();
end

-- 注册事件处理回调函数
function UIDragonWish:registerEventCallback()
    -- 注册许愿成功的事件
    EventMgr.register("UIDragonWish", event.WISH_DONE, function(args)
        -- 提示玩家许愿成功
        --self:alertWishSucc(args.bonus);

        -- 关闭界面
        UIMgr.getCurrentScene():removeFormByName("UIDragonWish");
    end);

    self:registerScriptHandler(function(eventType)
        if eventType == "exit" then
            cclog("UIDragonWish 界面析构清理");
            EventMgr.removeAll("UIDragonWish");
        end
    end);
end

-- 提示玩家许愿成功
function UIDragonWish:alertWishSucc(bonus)
    local desc = getBonusDesc(bonus);
    local msg = string.format(getLocStr("dragon_wish_succ"), desc);
    alert(msg);
end

-- 适配
function UIDragonWish:resize()
    local node = self.node;

    -- bg居中显示并充满整个屏幕
    AlignM.fitToScreen(node, "bg");

    -- CT居中并缩放至合适大小
    AlignM.alignToCenter(node, "CT");

    -- 特效节点居中缩放至合适大小
    AlignM.alignToCenter(node, "effect_node");
end

-- 重绘
function UIDragonWish:redraw()
    local CT = findChildByName(self.node, "CT");

    -- 显示tip
    local tip1Label = findChildByName(CT, "tip1");
    TextStyleM.setTextStyle(tip1Label, TextStyleM.TEXT_SIZE_SMALL, TextStyleM.TEXT_COLOR_DARK_BROWN);
    tip1Label:setString(getLocStr("dragon_wish_tip1"));

    local tip2Label = findChildByName(CT, "tip2");
    TextStyleM.setTextStyle(tip2Label, TextStyleM.TEXT_SIZE_SMALL, TextStyleM.TEXT_COLOR_DARK_BROWN);
    applyString(tip2Label, getLocStr("dragon_wish_tip2"));

    -- 显示选项
    local kerning = 0;
    if getLang() ~= "zh" and getLang() ~= "tw" then
        kerning = -1;
    end
    for i = 1, MAX_OPTION_NUM do
        local optionInfo = self.options[i];
        local btn = findChildByName(CT, "button" .. i);
        local desc = DragonWishM.getOptionDesc(optionInfo["id"]);
        TextStyleM.setTextStyle(btn, TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_BROWN_YELLOW, true, kerning);
        btn:setTitleText(desc);
        btn:setTag(i);

        -- 注册选项点击事件
        local function onClick(sender, eventType)
            if eventType == ccui.TouchEventType.ended then
                AudioM.playFx("button_click");

                -- 执行许愿
                local index = btn:getTag();

                local function doWish()
                    -- 如果没有配置夹层，直接执行许愿
                    local args = DragonWishM.query(optionInfo["id"], "args") or {};
                    if args["unreal"] == nil then
                        DungeonActionM.go("do_dragon_wish", 0, index);
                        UIMgr.getCurrentScene():removeFormByName("UIDragonWish")
                        return;
                    end

                    -- 显示传送中
                    showCommunicatingForm(getLocStr("delivering"));

                    -- 去除当前地牢的所有监听事件
                    EventMgr.removeAll("UIDungeonMain");

                    -- 这里需要稍微延迟一下，避免卡住
                    local function callback()
                        -- 许愿
                        DungeonActionM.go("do_dragon_wish", 0, index);

                        -- 关闭本界面
                        UIMgr.getCurrentScene():removeFormByName("UIDragonWish");
                    end
                    performWithDelay(self, callback, 0.5);
                end

                -- 抛出神龙冈布奥对话事件
                local dialogFormula = DragonWishM.query(optionInfo["id"], "formula");
                local option = DragonWishM.query(optionInfo["id"], "dialog");
                if dialogFormula ~= nil and "" ~= dialogFormula then
                    local dialogOption = FormulaM.invoke(dialogFormula, optionInfo["id"]);
                    if dialogOption then
                        option = DragonWishM.query(optionInfo["id"], "dialog2");
                    end
                end
                local msgList = string.explode(option, "|");
                EventMgr.fire(event.SLIME_TALK, {["heroId"] = 21, ["msgList"] = msgList,
                    ["endEffect"] = doWish});
                AudioM.playFx("slime_demon_app");

                -- 先隐藏界面
                self:setVisible(false);
            end
        end
        btn:addTouchEventListener(onClick);
    end
end

-- 初始化窗口
function UIDragonWish:initForm()
    -- 先将主窗口隐藏
    local CT = findChildByName(self.node, "CT");
    self.CT = CT;
    CT:setVisible(false);

    -- 播放神龙出现特效
    self:playDragonAppear();
end

-- 播放神龙出现特效
function UIDragonWish:playDragonAppear()
    local effectAnim = createEffectAnim(1210);

    local function callback()
        -- 窗口从中心位置出现
        local initScale = self.CT:getScale();
        self.CT:setVisible(true);
        self.CT:setScale(0.3*initScale);

        -- 先放大再缩小
        self.CT:stopAllActions();
        local referTime = 0.15;
        local upTime    = 1.5*referTime;
        local downTime  = 2*referTime;
        local scaleUp   = cc.ScaleTo:create(upTime, 1.1*initScale);
        local scaleDown = cc.ScaleTo:create(downTime, 0.95*initScale);

        -- 淡入
        -- self.CT:setOpacity(0);
        -- self.CT:setCascadeOpacityEnabled(true);
        -- local fadeIn = cc.FadeIn:create(upTime);

        self.CT:runAction(cc.Sequence:create(cc.Spawn:create(scaleUp), scaleDown));

        -- 播放神龙上移动画
        effectAnim:play(156, 203, false, 0.5);
    end

    -- 播放龙珠动动画
    effectAnim:play(0, 155, false, 0.45, callback);

    -- 龙珠音效
    AudioM.playFx("dragon_app");

    local effectNode = findChildByName(self.node, "effect_node");
    effectNode:addChild(effectAnim);
end




